//
//  ReadBookVOrH_VC+GetStoryCard.m
//  KidReading
//
//  Created by YangShuai on 16/11/18.
//  Copyright © 2016年 刘赞黄Telen. All rights reserved.
//

#import "ReadBookVOrH_VC+GetStoryCard.h"
#import "ConstDefine.h"

@interface ReadBookVOrH_VC()
@property(nonatomic,strong)UIButton* storyCardImageButton;
@end

@implementation ReadBookVOrH_VC (GetStoryCard)
ASSOCIATED(storyCardImageButton, setStoryCardImageButton, UIButton*, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

+ (void)load
{
    [self swizzleSelector:@selector(viewDidLoad) withSelector:@selector(viewDidLoad_getStoryCard)];
    [self swizzleSelector:@selector(readViewDidScrolledTo:) withSelector:@selector(readViewDidScrolledTo_getStoryCard:)];
}
//
- (void)viewDidLoad_getStoryCard
{
    [self viewDidLoad_getStoryCard];
    //下载本书需要的活动图片
    [self prepareStoryCardImage];
}
//
-(void)prepareStoryCardImage{
    UIButton* storyCardImageButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [storyCardImageButton addTarget: self action:@selector(tapStoryCard:) forControlEvents:UIControlEventTouchUpInside];
    [storyCardImageButton addTouchScaleTransitions];
    storyCardImageButton.adjustsImageWhenHighlighted = NO;
    storyCardImageButton.frame = CGRectMake([self randomNumberWithBegin:30 / ratio_w End:readView.width - 30 / ratio_w - 218 / ratio_w], [self randomNumberWithBegin:30 / ratio_w End:readView.height - 30 / ratio_w - 218 / ratio_w], 168 / ratio_w, 218 / ratio_w);
    [self.view addSubview:storyCardImageButton];
    self.storyCardImageButton = storyCardImageButton;
    storyCardImageButton.alpha = 0;
    storyCardImageButton.transform = CGAffineTransformMakeScale(0, 0);
}
//
-(void)showStoryCard{
    double rotation = [self randomRotation];
    [UIView animateWithDuration:0.6 delay:0 usingSpringWithDamping:0.5f initialSpringVelocity:0.9f options:UIViewAnimationOptionCurveEaseInOut animations:^{
        self.storyCardImageButton.transform = CGAffineTransformMakeScale(1, 1);
        self.storyCardImageButton.transform = CGAffineTransformMakeRotation(rotation);
        self.storyCardImageButton.alpha = 1;
    } completion:^(BOOL finished) {
        self.storyCardImageButton.transform = CGAffineTransformMakeScale(1, 1);
        self.storyCardImageButton.transform = CGAffineTransformMakeRotation(rotation);
        self.storyCardImageButton.alpha = 1;
    }];
}
-(void)unshowStoryCard{
    [UIView animateWithDuration:0.3 animations:^{
        self.storyCardImageButton.transform = CGAffineTransformMakeScale(0, 0);
        self.storyCardImageButton.alpha = 0;
    } completion:^(BOOL finished) {
        self.storyCardImageButton.transform = CGAffineTransformMakeScale(0, 0);
        self.storyCardImageButton.alpha = 0;
    }];
}
-(void)removeStoryCardWithBiggerAni{
    [UIView animateWithDuration:0.3 animations:^{
        self.storyCardImageButton.frame = CGRectMake(230 / ratio_w, 210 / ratio_w, 290 / ratio_w, 385 / ratio_w);
        self.storyCardImageButton.alpha = 0;
    } completion:^(BOOL finished) {
        self.storyCardImageButton.alpha = 0;
        [self.storyCardImageButton removeFromSuperview];
    }];
}
//
-(int)randomNumberWithBegin:(int)begin End:(int)end{
    return (int)(begin + (arc4random() % (end - begin + 1)));
}
//返回角度
-(double)randomRotation{
    double rotation = 0;
    NSInteger random = [self randomNumberWithBegin:1 End:4];
    if (random == 1) {
        rotation = -M_PI_4 / 3 * 2;
    }else if (random == 3){
        rotation = M_PI_4 / 3 * 2;
    }
    return rotation;
}
//

- (void)readViewDidScrolledTo_getStoryCard:(PageView *)pageView
{
    [self readViewDidScrolledTo_getStoryCard:pageView];
    if (self.storyCardInBook.success) {
        if (!self.storyCardImageButton.currentImage) {
            [self.storyCardImageButton JRSD_setImageWithURL:[NSURL URLWithString:self.storyCardInBook.storyCard.card_img_url] forState:UIControlStateNormal placeholderImage:nil];
        }
        if (pageView.pageIndex == self.storyCardInBook.page) {
            //出现卡牌
            [self showStoryCard];
        }else{
            [self unshowStoryCard];
        }
    }
}
//
-(void)tapStoryCard:(UIButton*)button{
    button.enabled = NO;
    //获取卡牌接口
    [[NetGetPostsWithCache shareInstance] getStoryCardInBookSuccessWithCardCode:self.storyCardInBook.storyCard.card_code CardPieceNum:self.storyCardInBook.cardPieceNums Success:^(NSDictionary *retDict) {
        //显示详情
        [self removeStoryCardWithBiggerAni];
        [GetStoryCardPopUpViewController showPopUpViewToActivityVC:self StoryCardName:self.storyCardInBook.storyCard.card_name CardCount:self.storyCardInBook.cardPieceNums StoryCardimage:self.storyCardImageButton.currentImage];
        button.enabled = YES;
        //
        [[RootViewController shareLastInstance] reflashHomePage];
        [[RootViewController shareLastInstance] refreshAboutMeStoryCard];
    } Failure:^(NSError *error) {
        button.enabled = YES;
        [UIAlertView tl_showTitle:@"" message:NSLocalizedString(@"needNetCacheGost", nil) didClick:^(UIAlertView * _Nullable alertView, NSInteger buttonIndex) {
        } buttonTitles:@"取消",nil];
    }];
    
}

@end
